package springapp.web;

import java.io.IOException;
import java.util.Set;

import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import springapp.entity.LocationEntity;
import springapp.service.CompanyService;
import springapp.service.IIndustryService;
import springapp.service.ILocationService;
import springapp.util.Utils;

@Controller
public class AddCompanyController implements IControllerConstants {

	protected final Log logger = LogFactory.getLog(getClass());

	@Autowired(required = true)
	private CompanyService companyService;

	@Autowired(required = true)
	private IIndustryService industryService;

	@Autowired(required = true)
	private ILocationService locationService;

	@RequestMapping(value = "/addCompanyD.htm", method = RequestMethod.POST)
	public @ResponseBody
	String handleRequest(@RequestParam("cName") String companyName,
			@RequestParam("hq") String hqName,
			@RequestParam("industry") String industry,
			@RequestParam(value = "location[]") String[] locations)
			throws ServletException, IOException {
		System.out.println("Printing " + companyName + " " + hqName + " "
				+ industry + " ");
		Integer industryId = industryService.getIndustryId(industry);
		Set<LocationEntity> locationEntities = locationService
				.getLocationEntities(locations);

		companyService.addCompany(companyName, hqName, industryId,
				locationEntities);

		return Utils.getJson(new Response(true));
	}
}